//ThreadWorker.java
/**
  * This is the worker class for the Update Thread.  It performs one breathfirst search of the file system starting at the scandir
  * and adds any new files to the application.
  */


import java.util.*;
import java.io.*;
import ca.odell.glazedlists.*;
import ca.odell.glazedlists.swing.*;
import javax.swing.filechooser.FileSystemView;

public class ThreadWorker{
   private Mp3Info mp3Info;
   public boolean useHiding = true;
   int maxLevel = 99;
   int c =0;
   EventList fileEventList;
   DBFile db;
   Object[] rootArray;
    LinkedList folders;

public ThreadWorker(EventList fileEventList){
   this.fileEventList = fileEventList;
   }

public ThreadWorker(EventList fileEventList, Object[] rootArray){
   this.rootArray = rootArray;
   this.fileEventList = fileEventList;
   }


  public void scanRecursive(File[] files, int currentLevel, int maxLevel, ConfigurationSingleton config) {
        LinkedList deleted = config.getDeletedFiles();
        c = c + 1;
        //System.out.println("count = "+c);
      if (files == null || files.length < 1) {
         return;
      }
      FileSystemView fileSystemView = FileSystemView.getFileSystemView();
      for (int i = 0; i < files.length; i++) {
         File file = files[i];
         DBFile d = new DBFile(file.getPath());
           Boolean trav = fileSystemView.isTraversable(file);
         boolean traversable = false;
         if (trav != null && trav.booleanValue()){
            traversable = true;
         }
         if (file.isFile() && (deleted.contains(d) == false)) {
         //if (file.isFile()) {
         maybeUpdateDB(file);

         }
         if (currentLevel < maxLevel && traversable) {
            scanRecursive(fileSystemView.getFiles(file,  useHiding), currentLevel + 1, maxLevel,config);
         }
      }
   }

public void scanIterative(ConfigurationSingleton config){
  LinkedList deleted = config.getDeletedFiles();
  folders = new LinkedList();
  for(int k=0;k<rootArray.length;k++){
   File rootFile = (File)rootArray[k];
   if(rootFile.listFiles() != null){
      folders.add(rootFile);
   }
  }
  File currentFile;
  DBFile d;
  while(folders.size()!= 0) {
       File nextFolder = (File)folders.removeFirst();
         File[] files = nextFolder.listFiles();
         for(int j=0;j<files.length;j++){
         currentFile = (File)files[j];
         d = new DBFile(currentFile.getPath());
         String currentFileName = currentFile.getName();
         String windowsDir = "WINDOWS";
         String oracleDir = "Oracle10g";
         String programFilesDir = "Program Files";
         String i386Dir = "i386";
         if(currentFile.isDirectory() && currentFile.isHidden() == false && windowsDir.equals(currentFileName) == false && i386Dir.equals(currentFileName) == false && oracleDir.equals(currentFileName) == false && programFilesDir.equals(currentFileName) == false){
              folders.add(currentFile);
            }

            else if(currentFile.isFile() && currentFile.isHidden() == false && deleted.contains(d) == false){
               maybeUpdateDB(currentFile);
            }

         }
       }
  }
   public void getTagInfo(DBFile db){
      if(db.getExt().equals("mp3") || db.getExt().equals("MP3")){
               mp3Info = new Mp3Info(db);
               mp3Info.setMp3();
      }
   }


   private void maybeUpdateDB(File file){
            String absPath = file.getPath();
            db = new DBFile(absPath);
            try {
              if(fileEventList.contains(db) == false){
                 System.out.println("New file found: "+absPath);
                 getTagInfo(db);
                   fileEventList.getReadWriteLock().writeLock().lock();
               fileEventList.add(fileEventList.size(),db);
                fileEventList.getReadWriteLock().writeLock().unlock();
                }
              }
              catch (Exception se) {se.printStackTrace();}
   }
}

